package com.剑指offer;

public class _1二维数组中的查找
{
    public static void main(String[] args)
    {
        System.out.println("1多了一个输出");
        int a[][] = new int[][]{
            {1,   4,  7, 11, 15},
            {2,   5,  8, 12, 19},
            {3,   6,  9, 16, 22},
            {10, 13, 14, 17, 24},
            {18, 21, 23, 26, 30}
        };
        System.out.println(findNumberIn2DArray(a,8));
    }

    public static boolean findNumberIn2DArray(int[][] matrix, int target)
    {
        for (int i = 0; i < matrix.length; i++)
        {
            int low = 0;
            int high = matrix[i].length - 1;
            while (low <= high)
            {
                int mid = (low + high) / 2;
                if (target > matrix[i][mid])
                {
                    low = mid + 1;
                }
                else if (target < matrix[i][mid])
                {
                    high = mid - 1;
                }
                else
                {
                    return true;
                }
            }
        }
        return false;
    }
}
